package com.test.quartz.controller;

import com.test.quartz.memory.job.Job1;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 动态创建任务
 */
@RestController
@RequestMapping("/memory")
public class MemoryController {

    @Autowired
    private Scheduler scheduler;

    @RequestMapping("/scheduler")
    public void test1() throws SchedulerException {
        String schedulerName = scheduler.getSchedulerName();
        System.out.println(schedulerName);
    }

    @RequestMapping("/start")
    public void test2() throws SchedulerException {
        JobDetail jodDetail = JobBuilder.newJob(Job1.class)
                .withIdentity("job1", "group1")
                .storeDurably()
                .build();

        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("trigger1", "group1")
                .forJob(jodDetail)
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule("0/3 * * * * ?"))
                .build();
        scheduler.scheduleJob(jodDetail, trigger);
    }

    @RequestMapping("/pause")
    public void test3() throws SchedulerException {
        JobKey jobKey = new JobKey("job1", "group1");
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail == null){
            System.out.println("job1 不存在");
        }else {
            scheduler.pauseJob(jobKey);
            System.out.println("job1 暂停成功");
        }
    }

    @RequestMapping("/resume")
    public void test4() throws SchedulerException {
        JobKey jobKey = new JobKey("job1", "group1");
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail == null){
            System.out.println("job1 不存在");
        }else {
            scheduler.resumeJob(jobKey);
            System.out.println("job1 恢复成功成功");
        }
    }


}
